module.exports = function ($resource, $state, $http, $q, FileUploader, toaster) {

	return {
		restrict: 'AE',
		template: require('../views/productinfoPriceStandard.html'),
		replace: true,
		scope: {
			'saleobj': '=',
			'funobj': '=',
			'baseinfo': '=',
			'util': '=',
		},
		link: function (scope, elements, attrs){
			scope.tabledata = [];

			function getSKUlist() {
				$resource('/api/ac/tc/ticketSaleSKUService/getSaleSKUList', {}, {})
				  .save({'sale_code':scope.saleobj.code}, function(res){
					if (res.errcode != 0) {
						toaster.error({ title: '', body: '获取产品规格信息失败'});
						return;
					}
					scope.priceMap = res.data.price_list;
					//规格列表转为二维数组
					scope.skuArray = [];
					res.data.sku_list.forEach(element => {
						if(element.is_check == '1'){
							var alength = scope.skuArray.length;
							if(alength == 0 || element.sku_type != scope.skuArray[alength-1].sku_type){
								scope.skuArray.push({'id':element.sku_type_sort,'sku_type': element.sku_type, 'list': []});
							}
							alength = scope.skuArray.length;
							scope.skuArray[alength-1].list.push({'id': element.id, 'sku_name': element.sku_name});
						}
					});
					var i = scope.skuArray.length - 1;
					var end = 1;
					for(; i >= 0; i--){
						var temp1 = scope.skuArray[i];
						angular.forEach(temp1.list, function(temp2, index2){
							if(i == scope.skuArray.length - 1){
								var data1 = {};
								data1['name'+temp1.id] = temp2.sku_name;
								data1['rowspan'+temp1.id] = 1;
								data1.sku_key = temp2.sku_name;
								scope.tabledata.push(data1);
							} else {
								if(index2 == 0) end *= scope.skuArray[i+1].list.length;
								for(var j = 0; j < end; j++){
									var temp3 = scope.tabledata[j];
									if(temp3['name'+temp1.id]){
										var temp4 = angular.copy(temp3);
										temp4['name'+temp1.id] = temp2.sku_name;
										var index = temp3.sku_key.indexOf(',');
										temp4.sku_key = temp2.sku_name + ',' + temp3.sku_key.substring(index+1);
										scope.tabledata.push(temp4);
									} else {
										temp3['name'+temp1.id] = temp2.sku_name;
										temp3['rowspan'+temp1.id] = end;
										temp3.sku_key = temp2.sku_name + ',' + temp3.sku_key;
									}
								}
							}
						});
					}
				});
			}
			getSKUlist();
			


		}

	};
};

